#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "com.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "keypane.h"
#include "info.h"

//#define INFO_TEST
/*
 *
 */
void SetInfoModal(HWND hWnd, BOOL bModal)
{
    int style = GetWindowAdditionalData(hWnd);
    if(bModal)
    {
        style &= ~INFO_NOT_MODAL;
    }
    else
    {
        style |= INFO_NOT_MODAL;
    }
    SetWindowAdditionalData(hWnd, style);
}
/*
 *
 */
void SetInfoText(HWND hWnd, const char *info)
{
    HWND hWndTmp = GetDlgItem(hWnd, IDC_TIP);
    if(hWndTmp)
    {
        SetWindowCaption(hWndTmp, info);
        InvalidateRect(hWndTmp, NULL, TRUE);
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    int style = GetWindowAdditionalData(hWnd);
    switch(nID)
    {
    case ID_ESC:
        if(!GetDlgItem(hWnd, IDC_TIP))
        {
            printf("no window ctrl: %d\n", nID);
        }
        else if(style & INFO_NOT_MODAL)
        {
            ShowWindow(hWnd, SW_HIDE);
        }
        else
        {
#ifdef INFO_TEST
            PostQuitMessage(hWnd);
#else
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
#endif
        }
        break;
    case ID_ENTER:
        if(!GetDlgItem(hWnd, IDC_TIP))
        {
            printf("no window ctrl: %d\n", nID);
        }
        else if(style & INFO_NOT_MODAL)
        {
            ShowWindow(hWnd, SW_HIDE);
        }
        else
        {
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    switch(key)
    {
    case SCANCODE_ESCAPE:
        OnBtnCmd(hWnd, ID_ESC);
        break;
    case SCANCODE_ENTER:
        OnBtnCmd(hWnd, ID_ENTER);
        break;
    }
}
/*
 *
 */
static int InfoDlgProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            RECT rect;
            GetClientRect(hWnd, &rect);
            int width = rect.right - rect.left;
            HWND hTmp = CreateWindow (MH_EDIT_NAME,
                "",
                WS_CHILD | WS_VISIBLE | BS_CENTER,
                IDC_TIP,
                20,
                60,
                width - 40,
                20,
                hWnd, 0);
            EnableWindow(hTmp, FALSE);
        }
        break;
    case MSG_PAINT:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.top += MASTER_TITLE_TOP;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            if(data.info.color)
            {
                SetTextColor(hdc, COLOR_HQ_RED(hdc));
            }
            else
            {
                SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
            }
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
#ifndef INFO_TEST
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
#endif
        }
        return 0;
    case MSG_DESTROY:
        {
#ifndef INFO_TEST
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
#endif
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, data.info.len ? &g_bmp[BMP_INFO_LEN] : &g_bmp[BMP_INFO]);
        }
        return 0;
    case MSG_LBUTTONDOWN:
        {
            OnBtnCmd(hWnd, ID_ESC);
        }
        break;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateInfoDialog(HWND hHosting, int style)
{
    WND_EXTRA_DATA data;
    data.value = 0;
    int cx = CX_INFO_BMP;
    int cy = CY_INFO_BMP;
    if(style & INFO_BMP_LEN)
    {
        cx = CX_INFO_LEN_BMP;
        cy = CY_INFO_LEN_BMP;
        data.info.len = TRUE;
    }
    int x = (CX_SCREEN - cx) / 2;
    int y = (CY_SCREEN - cy) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_NONE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_TIP_TITLE_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = InfoDlgProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + cx;
    CreateInfo.by = y + cy;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = data.value;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        ShowWindow(hWnd, SW_HIDE);
    }
    return hWnd;
}
/*
 *
 */
int InfoDoModal(HWND hWnd)
{
    int ret = 0;
    MSG Msg;
    HWND hHosting = GetHosting(hWnd);
    if(hHosting > 0)
    {
        EnableWindow(hHosting, FALSE);
    }
    if (hWnd > 0)
    {
        ShowWindow(hWnd, SW_SHOWNORMAL);
        while(GetMessage(&Msg, hWnd))
        {
#ifndef INFO_TEST
            if(Msg.hwnd == hWnd && Msg.message == MSG_CLOSE)
            {
                WND_EXTRA_DATA data;
                data.value = GetWindowAdditionalData(hWnd);
                ret = data.info.ret;
                break;
            }
#endif
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
#ifdef INFO_TEST
        printf("sss1\n");
        DestroyAllControls(hWnd);
        printf("sss2\n");
        DestroyMainWindow(hWnd);
        printf("sss3\n");
        ThrowAwayMessages(hWnd);
        printf("sss4\n");
        MainWindowThreadCleanup(hWnd);
        printf("sss5\n");
#endif

        if(hHosting > 0)
        {
            EnableWindow(hHosting, TRUE);
            ShowWindow(hHosting, SW_SHOWNORMAL);
            SetActiveWindow(hHosting);
            SendMessage(hWnd, MSG_CLOSE, 0, 0);
        }
    }
    return ret;
}
/*
 *
 */
int AfxInfoBox(HWND hWnd, const char *title, const char *sc)
{
    int len = 0;
    if(sc)
    {
        len = strlen(sc);
    }
    HWND hDlg = CreateInfoDialog(hWnd, len > 20 ? INFO_BMP_LEN : 0);
    if(title)
    {
        SetWindowCaption(hDlg, title);
    }
    if(sc)
    {
        SetInfoText(hDlg, sc);
    }
    return InfoDoModal(hDlg);
}
